﻿using NPOI.HSSF.UserModel;
using OASystem.BLL;
using OASystem.BLL.Service;
using OASystem.Model;
using OASystem.Models;
using OASystem.ViewModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace OASystem.Controllers
{
    public class PersonController : Controller
    {
       public static Userinfo user;
        private PersonService Service = new PersonService();
      /// <summary>
      /// 调用分页
      /// </summary>
      /// <param name="page"></param>
      /// <param name="limit"></param>
      /// <param name="username"></param>
      /// <returns></returns>
        public JsonResult UserPageList(int page, int limit,string username)
        {
            ResponseResult responseResult = new ResponseResult();
            responseResult.data = Service.UserPageList(page, limit, username);
            return Json(responseResult.data, JsonRequestBehavior.AllowGet);
        }
        ///// <summary>
        ///// 删除用户信息
        ///// </summary>
        ///// <param name="userinfo"></param>
        ///// <returns></returns>
        //public JsonResult DelectUser(Userinfo userinfo)
        //{
        //    Operate operate = new Operate();
        //    operate.Success =Service.Delete(userinfo);
        //    return Json(operate);
        //}
        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <param name="function"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult UpdateUser(Userinfo userinfo)
        {
            Operate operate = new Operate();
            operate.Success = Service.Update(userinfo);
            return Json(operate);
        }
        /// <summary>
        /// 显示编辑数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult Update(int id)
        {
            ResponseResult responseResult = new ResponseResult();
            responseResult = Service.Update(id);
            user = responseResult.data as Userinfo;
            return Json(responseResult);
        }
        /// <summary>
        /// 多表删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult Delect(int id)
        {
            //创建操作类对象 
            ResponseResult operateResult = new ResponseResult();
            //创建业务层对象
            BaseService<Userinfo> user = new BaseService<Userinfo>();
            BaseService<Adminuserrole> role = new BaseService<Adminuserrole>();
            //定义查询拉姆达表达式
            Expression<Func<Userinfo, bool>> orderinfoLambda = a => a.UserId == id;
            Expression<Func<Adminuserrole, bool>> orderdetailLambda = a => a.UserId == id;
            //调用查询返回一个对象
            Userinfo orderinfoList = user.Query(orderinfoLambda);
            Adminuserrole orderdetailList = role.Query(orderdetailLambda);
            //调用删除方法
            operateResult.data= Service.Delect(orderinfoList, orderdetailList);
            return Json(operateResult);
        }
       /* /// <summary>
        /// 多表编辑
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult EditUserInfo(int id)
        {
            ResponseResult responseResult = new ResponseResult();
            BaseService<Userinfo> userinfo = new BaseService<Userinfo>();
            BaseService<Adminuserrole> adminuserrole = new BaseService<Adminuserrole>();
            //定义查询拉姆达表达式
            Expression<Func<Userinfo, bool>> orderinfoLambda = a => a.UserId == id;
            Expression<Func<Adminuserrole, bool>> orderdetailLambda = a => a.UserId == id;
            //调用查询返回一个对象
            Userinfo orderinfoList = userinfo.Query(orderinfoLambda);
            Adminuserrole orderdetailList = adminuserrole.Query(orderdetailLambda);
            responseResult.data= Service.EditUserInfo(orderinfoList, orderdetailList);
            return Json(responseResult);
        }*/
        public ActionResult Excel()
        {
            List<Userinfo> task = new List<Userinfo>();
            ResponseResult result = new ResponseResult();
            Expression<Func<Userinfo, bool>> whereLambda = a =>true;
            result= Service.Excel();
            task = result.data;
            var sbHtml = new StringBuilder();
            sbHtml.Append("<table border='1' cellspacing='0' cellpadding='0'>");
            sbHtml.Append("<tr>");
            var lstTitle = new List<string> { "用户Id", "用户名", "用户密码", "部门Id", "状态", "真实姓名", "备注","创建时间" };
            foreach (var item in lstTitle)
            {
                sbHtml.AppendFormat("<td style='font-size: 14px;text-align:center;background-color:#DCE0E2;font-weight:bold;'height='25'>{0}</td>", item);
            }
            sbHtml.Append("</tr>");

            for (int i = 0; i < task.Count; i++)
            {
                sbHtml.Append("<tr>");
                sbHtml.AppendFormat("<td>{0}</td>", task[i].UserId);
                sbHtml.AppendFormat("<td>{0}</td>", task[i].UserName);
                sbHtml.AppendFormat("<td>{0}</td>", task[i].Password);
                sbHtml.AppendFormat("<td>{0}</td>", task[i].DepartId);
                sbHtml.AppendFormat("<td>{0}</td>", task[i].Status);
                sbHtml.AppendFormat("<td>{0}</td>", task[i].EmployeeName);
                sbHtml.AppendFormat("<td>{0}</td>", task[i].Remark);
                sbHtml.AppendFormat("<td>{0}</td>", task[i].CreateTime);
                sbHtml.Append("</tr>");
            }
            sbHtml.Append("</table>");
            byte[] fileContents = Encoding.Default.GetBytes(sbHtml.ToString());
            return File(fileContents, "application/ms-excel", "员工信息.xls");
        }
    }
}